package advertising

import (
	"context"

	"advertising/config/mysql"
	"advertising/internal/app/grpc/protoc/advertisingc"
	"advertising/internal/app/web/service"
)

// Save 素材上传
func (s *Server) MMaterialSave(ctx context.Context, req *advertisingc.MMaterialSaveReq) (*advertisingc.MMaterialSaveRsp, error) {
	res := &advertisingc.MMaterialSaveRsp{}

	if req.Name == "" {
		res.Code = -1
		res.Msg = "素材名称不能为空"
		return res, nil
	}

	if req.Type != 1 && req.Type != 2 {
		res.Code = -1
		res.Msg = "素材类型错误"
		return res, nil
	}

	if req.Position != 1 && req.Position != 2 {
		res.Code = -1
		res.Msg = "素材位置错误"
		return res, nil
	}

	if req.FilePath == "" {
		res.Code = -1
		res.Msg = "文件路径不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	id, filepath, err := service.Material{}.Save(ctx, db, req.MerchantId, req.MerchantName, req.UserId,
		req.Username, req.Name, req.FilePath, int8(req.Position), int8(req.Type), req.FolderId)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Id = id
	res.FilePath = filepath
	res.Code = 0
	res.Msg = "保存成功"
	return res, nil
}

// List 素材列表
func (s *Server) MMaterialList(ctx context.Context, req *advertisingc.MMaterialListReq) (*advertisingc.MMaterialListRsp, error) {
	res := &advertisingc.MMaterialListRsp{}

	if req.Type != 0 && req.Type != 1 && req.Type != 2 {
		res.Code = -1
		res.Msg = "素材类型错误"
		return res, nil
	}

	if req.Position != 0 && req.Position != 1 && req.Position != 2 {
		res.Code = -1
		res.Msg = "素材位置错误"
		return res, nil
	}

	if req.Status < 0 || req.Status > 4 {
		res.Code = -1
		res.Msg = "素材状态错误"
		return res, nil
	}

	db := mysql.NewDB()
	list, count, err := service.Material{}.List(ctx, db, req.MerchantId, int8(req.Type), req.Name,
		int(req.Position), int8(req.Status), int(req.Page), int(req.Limit), req.FolderId)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.Data = &advertisingc.MMaterialListData{
		Count: count,
		List:  make([]*advertisingc.MMaterialItem, 0),
	}

	for _, item := range list {
		res.Data.List = append(res.Data.List, &advertisingc.MMaterialItem{
			Id:             item.ID,
			Name:           item.Name,
			FilePath:       item.FilePath,
			Position:       int32(item.Position),
			Type:           int32(item.Type),
			Status:         int32(item.Status),
			FolderId:       item.FolderID,
			MerchantId:     item.MerchantID,
			MerchantName:   item.MerchantName,
			CreateUser:     item.CreateUser,
			CreateUsername: item.CreateUserName,
			CreatedAt:      item.CreatedAt,
		})
	}

	return res, nil
}

// Delete 素材删除
func (s *Server) MMaterialDelete(ctx context.Context, req *advertisingc.MMaterialDeleteReq) (*advertisingc.MMaterialDeleteRsp, error) {
	res := &advertisingc.MMaterialDeleteRsp{}

	if req.Ids == "" {
		res.Code = -1
		res.Msg = "素材ID不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	err := service.Material{}.Delete(ctx, db, req.MerchantId, req.Ids)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "删除成功"
	return res, nil
}

// MaterialInUseList 可用素材列表
func (s *Server) MaterialInUseList(ctx context.Context, req *advertisingc.MMaterialInUseListReq) (*advertisingc.MMaterialInUseListRsp, error) {
	res := &advertisingc.MMaterialInUseListRsp{}

	if req.Type != 0 && req.Type != 1 && req.Type != 2 {
		res.Code = -1
		res.Msg = "素材类型错误"
		return res, nil
	}

	if req.Position != 0 && req.Position != 1 && req.Position != 2 {
		res.Code = -1
		res.Msg = "素材位置错误"
		return res, nil
	}

	db := mysql.NewDB()
	list, count, err := service.Material{}.MaterialInUseList(ctx, db, req.MerchantId, int8(req.Type), req.Name,
		int(req.Position), int(req.Page), int(req.Limit), req.FolderId)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.Data = &advertisingc.MMaterialInUseListData{
		Count: count,
		List:  make([]*advertisingc.MMaterialInUseItem, 0),
	}

	for _, item := range list {
		res.Data.List = append(res.Data.List, &advertisingc.MMaterialInUseItem{
			Id:       item.ID,
			Name:     item.Name,
			FilePath: item.FilePath,
			Position: int32(item.Position),
			Type:     int32(item.Type),
			FolderId: item.FolderID,
		})
	}

	return res, nil
}

// AuditingMaterialCount 审核中素材统计
// 注意：proto中没有merchant_id字段，这里需要从上下文或其他地方获取
// 建议修改proto定义，添加merchant_id字段
func (s *Server) AuditingMaterialCount(ctx context.Context, req *advertisingc.MAuditingMaterialCountReq) (*advertisingc.MAuditingMaterialCountRsp, error) {
	res := &advertisingc.MAuditingMaterialCountRsp{}

	// 由于proto中没有merchant_id字段，这里暂时返回错误
	// 实际使用时需要修改proto定义或从上下文中获取商户ID
	res.Code = -1
	res.Msg = "需要商户ID参数，请修改proto定义添加merchant_id字段"
	return res, nil
}

// MoveFolder 素材移动文件夹
func (s *Server) MoveFolder(ctx context.Context, req *advertisingc.MMaterialMoveFolderReq) (*advertisingc.MMaterialMoveFolderRsp, error) {
	res := &advertisingc.MMaterialMoveFolderRsp{}

	if req.Ids == "" {
		res.Code = -1
		res.Msg = "素材ID不能为空"
		return res, nil
	}

	if req.FolderId == "" {
		res.Code = -1
		res.Msg = "目标文件夹ID不能为空"
		return res, nil
	}

	err := service.Material{}.MoveFolder(ctx, req.MerchantId, req.Ids, req.FolderId)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "移动成功"
	return res, nil
}

// FolderSave 文件夹保存
func (s *Server) FolderSave(ctx context.Context, req *advertisingc.MFolderSaveReq) (*advertisingc.MFolderSaveRsp, error) {
	res := &advertisingc.MFolderSaveRsp{}

	if req.Name == "" {
		res.Code = -1
		res.Msg = "文件夹名称不能为空"
		return res, nil
	}

	if req.Type != 1 && req.Type != 2 {
		res.Code = -1
		res.Msg = "文件夹类型错误"
		return res, nil
	}

	err := service.MaterialFolder{}.FolderSave(ctx, req.MerchantId, req.Id, req.Pid, int8(req.Type), req.Name)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "保存成功"
	return res, nil
}

// FolderList 文件夹列表
func (s *Server) FolderList(ctx context.Context, req *advertisingc.MFolderListReq) (*advertisingc.MFolderListRsp, error) {
	res := &advertisingc.MFolderListRsp{}

	if req.Type != 1 && req.Type != 2 {
		res.Code = -1
		res.Msg = "文件夹类型错误"
		return res, nil
	}

	list, err := service.MaterialFolder{}.FolderList(ctx, req.MerchantId, int8(req.Type))

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.Data = make([]*advertisingc.MFolderItem, 0)

	for _, item := range list {
		res.Data = append(res.Data, &advertisingc.MFolderItem{
			Id:        item.ID,
			Pid:       item.PID,
			Type:      int32(item.Type),
			Name:      item.Name,
			Sort:      int32(item.Sort),
			CreatedAt: item.CreatedAt,
		})
	}

	return res, nil
}

// FolderDelete 文件夹删除
func (s *Server) FolderDelete(ctx context.Context, req *advertisingc.MFolderDeleteReq) (*advertisingc.MFolderDeleteRsp, error) {
	res := &advertisingc.MFolderDeleteRsp{}

	if req.Id == "" {
		res.Code = -1
		res.Msg = "文件夹ID不能为空"
		return res, nil
	}

	err := service.MaterialFolder{}.FolderDelete(ctx, req.MerchantId, req.Id)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "删除成功"
	return res, nil
}

// FolderSort 文件夹排序
func (s *Server) FolderSort(ctx context.Context, req *advertisingc.MFolderSortReq) (*advertisingc.MFolderSortRsp, error) {
	res := &advertisingc.MFolderSortRsp{}

	if len(req.Sort) == 0 {
		res.Code = -1
		res.Msg = "排序数据不能为空"
		return res, nil
	}

	// 转换排序数据格式
	sortData := make([]struct {
		ID   string `json:"id"`
		Sort int8   `json:"sort"`
	}, 0)

	for _, item := range req.Sort {
		sortData = append(sortData, struct {
			ID   string `json:"id"`
			Sort int8   `json:"sort"`
		}{
			ID:   item.Id,
			Sort: int8(item.Sort),
		})
	}

	err := service.MaterialFolder{}.FolderSort(ctx, req.MerchantId, sortData)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "排序成功"
	return res, nil
}
